import { http } from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id: 0,
        value: "综合",
        isActive: true
      },
      {
        id: 1,
        value: "销量",
        isActive: false
      },
      {
        id: 2,
        value: "价格",
        isActive: false
      }
    ],
    goodsList: []
  },
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  totalPages:1,
  onLoad: function (options) {
    this.QueryParams.cid = options.cid;
    this.getGoodsList()
  },
  async getGoodsList() {
    const res = await http({url:'/goods/search',data: this.QueryParams});
    console.log(res)
    const total = res.total;
    //总页数
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize);
    this.setData({
      goodsList: [...this.data.goodsList,...res.goods]
    });

    //关闭下拉刷新
    wx.stopPullDownRefresh()
  },
  //父组件监听 改变tab切换
  handleTabsItem(e) {
    // console.log(e)
    const {index} = e.detail;
    const {tabs} = this.data;
    tabs.forEach((v,i)=> i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },

  //触底事件  上拉加载
  //用户上画页面 滚动条触底 开始加载下一页数据
  // 判断有没有下一页数据 1.获取总页数 2.获取当前页码 3.判断一下 当前页码是否大于等于 总页数
  onReachBottom() {
    if(this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({
        title: '没有更多数据了',
      })
    }else {
      this.QueryParams.pagenum++;
      this.getGoodsList()
    }
  },
  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      goodsList: []
    });
    this.QueryParams.pagenum = 1;
    this.getGoodsList();
  }
})